package com.jamin.aabill.db;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.jamin.aabill.utils.Utils;

public class BillDataBaseHelper extends SQLiteOpenHelper {


	//database name
	public static final String DATABASE_NAME = "bill.db";

	/**
	 * DB version v0.1;Create bill.db
	 * 
	 */
	public static final int DATABASE_VERSION = 2;



	private static final String TAG = "BlackTeaDataBaseHelper=========wangjieming";
	private static BillDataBaseHelper mHelper;

	
	public synchronized static BillDataBaseHelper getInstance(Context ctx) {
		if (mHelper == null) {
			return new BillDataBaseHelper(ctx);
		} else {
			return mHelper;
		}
	}


	private BillDataBaseHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}




	private BillDataBaseHelper(Context context) {
		super(context, DATABASE_NAME, null,
				DATABASE_VERSION);
	}



	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(UserInfoTableMetaData.CREATE_TABLE_SQL);
		Utils.log(TAG, UserInfoTableMetaData.CREATE_TABLE_SQL );
		db.execSQL(CircleTableMetaData.CREATE_TABLE_SQL);
		Utils.log(TAG, CircleTableMetaData.CREATE_TABLE_SQL );
		db.execSQL(CircleMemberTableMetaData.CREATE_TABLE_SQL);
		Utils.log(TAG, CircleMemberTableMetaData.CREATE_TABLE_SQL );
		db.execSQL(BillTableMetaData.CREATE_TABLE_SQL);
		Utils.log(TAG, BillTableMetaData.CREATE_TABLE_SQL );
		db.execSQL(BillDetailTableMetaData.CREATE_TABLE_SQL);
		Utils.log(TAG, BillDetailTableMetaData.CREATE_TABLE_SQL );
		db.execSQL(BillViewMetaData.CREATE_VIEW_SQL);
		Utils.log(TAG, BillViewMetaData.CREATE_VIEW_SQL );
	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//删除UserInfo表
		db.execSQL(UserInfoTableMetaData.DROP_TABLE_SQL);
		Utils.log(TAG, UserInfoTableMetaData.DROP_TABLE_SQL );
		//删除Circle表
		db.execSQL(CircleTableMetaData.DROP_TABLE_SQL);
		Utils.log(TAG, CircleTableMetaData.DROP_TABLE_SQL );
		//删除CircleMember表
		db.execSQL(CircleMemberTableMetaData.DROP_TABLE_SQL);
		Utils.log(TAG, CircleMemberTableMetaData.DROP_TABLE_SQL );
		//删除Bill表
		db.execSQL(BillTableMetaData.DROP_TABLE_SQL);
		Utils.log(TAG, BillTableMetaData.DROP_TABLE_SQL );
		//删除BillDetail表
		db.execSQL(BillDetailTableMetaData.DROP_TABLE_SQL);
		Utils.log(TAG, BillDetailTableMetaData.DROP_TABLE_SQL );
		//删除Bill视图
		db.execSQL(BillViewMetaData.DROP_VIEW_SQL);
		Utils.log(TAG, BillViewMetaData.DROP_VIEW_SQL );
		//重建表
		onCreate(db);
	}

}


